package cn.school.mybatis.service;

import cn.school.mybatis.dao.StudentDao;
import cn.school.mybatis.pojo.Student;
import cn.school.mybatis.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//业务逻辑bean
//服务层,调用dao方法
public class StudentService {

    public final static StudentService STUDENT_SERVICE = new StudentService();

//增加
public int add(Student student) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        StudentDao dao = session.getMapper(StudentDao.class);
        return dao.add(student);
    } finally {
        session.commit();
        MyBatisUtil.close(session);
    }
}
//删除
public int del(int stuno) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        StudentDao dao = session.getMapper(StudentDao.class);
        return dao.del(stuno);
    } finally {
        session.commit();
        MyBatisUtil.close(session);
    }
}
//修改
public int update(Student student) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        StudentDao dao = session.getMapper(StudentDao.class);
        return dao.update(student);
    } finally {
        session.commit();
        MyBatisUtil.close(session);
    }
}
//查单个
public Student get(int stuno) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        StudentDao dao = session.getMapper(StudentDao.class);
        return dao.get(stuno);
    } finally {
        MyBatisUtil.close(session);
    }
}
//查全部

//查数量
//模糊查全部
//模糊查数量
public int getSearchCount(Map map) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        StudentDao studentDao = session.getMapper(StudentDao.class);
        int count = studentDao.fuzzySearchCount(map);
        return count;
    } finally {
        MyBatisUtil.close(session);
    }
}
//分页查询
//模糊分页查询
    public List<Student> fuzzySerch(int pageNo, int pageSize, String orderBy,Map<String,Object> condition) {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao ;
        int offset = (pageNo - 1) * pageSize;
        offset = Math.max(offset, 0);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", offset);
        map.put("pageSize", pageSize);
        map.put("orderBy", orderBy);

        if (condition.size()>0) {
            for (String key : condition.keySet()) {
                map.put(key, condition.get(key));
            }
        }

        try {
            studentDao = session.getMapper(StudentDao.class);
            List<Student> list = studentDao.fuzzySarch(map);
            return list;
        } finally {
            MyBatisUtil.close(session);
        }
    }

//分页查找
//    public int searchTotal(Map<String, Object> params, int pageSize) {
//        SqlSession session = MyBatisUtil.getSqlSession();
//        try {
//            StudentDao dao = session.getMapper(StudentDao.class);
//            int total = dao.searchTotal(params);
//            // total/pageSize+(total%pageSize==0?0:1);
//
//            return (total - 1) / pageSize + 1;
//        } finally {
//            MyBatisUtil.close(session);
//        }
//    }


}
